package com.elock.intf.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.ConvertUtil;
import com.elock.entity.Terminal;
import com.elock.intf.service.IntfTerminalMessageSendService;
import com.elock.intf.service.IntfTerminalService;
import com.elock.service.CompanyInfoService;

/**
 * Controller - 终端设备
 * @author 仁德
 */
@Controller("intfTerminalController")
@RequestMapping("/intf/terminal")
public class TerminalController extends BaseController {

	@Resource(name = "intfTerminalServiceImpl")
	private IntfTerminalService intfTerminalService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;
	@Resource(name = "intfTerminalMessageSendServiceImpl")
	private IntfTerminalMessageSendService intfTerminalMessageSendService;

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String companycode, String terminalNum,
			String terminalName, String sim, String saleOrgSn, Pageable pageable,
			ModelMap model) {

		if (ConvertUtil.isEmpty(companycode)) {
			return error("请传入企业编号");
		}
		if (!companyInfoService.exists(Filter.eq("company_code", companycode))) {
			return error("不存在企业编号为[" + companycode + "]的企业");
		}

		Object[] args = { companycode, terminalNum, terminalName, saleOrgSn, sim };
		Page<Map<String, Object>> page = intfTerminalService.findPage(pageable, args);

		return success().addObjX(page);
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(String companycode, Terminal terminal,
			String saleOrgSn) {

		intfTerminalService.update(companycode, terminal, saleOrgSn);

		return success();
	}

	/**
	 * 信息下发
	 */
	@RequestMapping(value = "/send_ctrl", method = RequestMethod.POST)
	public @ResponseBody ResultMsg send_ctrl(String companycode, String terminalNum,
			String content, HttpServletRequest request) throws Exception {

		Long id = intfTerminalMessageSendService.sendMsg(companycode,
				terminalNum,
				34304,
				content,
				request);

		return success().addObjX(id);
	}

	/**
	 * 信息下发
	 */
	@RequestMapping(value = "/send_parm", method = RequestMethod.POST)
	public @ResponseBody ResultMsg send_parm(String companycode, String terminalNum,
			String content, HttpServletRequest request) throws Exception {

		Long id = intfTerminalMessageSendService.sendMsg(companycode,
				terminalNum,
				33027,
				content,
				request);

		return success().addObjX(id);
	}
}
